/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controlador;

import Conexion.Cliente;
import Dominio.Bala;
import Dominio.Obstaculo;
import Dominio.Tank;
import java.awt.Graphics;
import java.util.LinkedList;
import javax.swing.ImageIcon;

/**
 *
 * @author Sebastian
 */
public class Controlador extends javax.swing.JPanel {
    
    private ImageIcon stage; 
    private LinkedList <Obstaculo> lstObs;
    private LinkedList <Bala> lstBalas;
    private Tank tankOne;
    private Cliente cli;

    /**
     * Creates new form Controlador
     */
    public Controlador() {
        initComponents();
        this.stage = new ImageIcon(getClass().getResource("../Imagenes/Stage/stageVacio.png"));
        this.lstBalas = new LinkedList<>();
    }
    
    public void recibir(String [] rutasTank, LinkedList<Obstaculo> lstObs){
        this.tankOne = new Tank(10, 10, 36, 36, rutasTank);
        this.lstObs = lstObs;
   //    this.cli = new Cliente();
    }
    
    public void disparo(String [] rutasBala){
        Bala b;
        if (this.tankOne.getDireccion() == 0) {
            b = new Bala(this.tankOne.getX()+this.tankOne.getAncho()-5, 
                    this.tankOne.getY()+(this.tankOne.getAlto()/2)-5, 10, 10, rutasBala, this.lstObs);
        }
        else if(this.tankOne.getDireccion() == 1){
            b = new Bala(this.tankOne.getX(), 
                    this.tankOne.getY()+(this.tankOne.getAlto()/2)-5, 10, 10, rutasBala, this.lstObs);
        }
        else if(this.tankOne.getDireccion() == 2){
            b = new Bala(this.tankOne.getX()+(this.tankOne.getAncho()/2)-5, 
                    this.tankOne.getY()-5, 10, 10, rutasBala, this.lstObs);
        }
        else{
            b = new Bala(this.tankOne.getX()+(this.tankOne.getAncho()/2)-5, 
                    this.tankOne.getY()+this.tankOne.getAlto()-5, 10, 10, rutasBala, this.lstObs);
        }
        b.setDireccion(this.tankOne.getDireccion());
        this.lstBalas.add(b);
        Thread hilo = new Thread(b);
        hilo.start();
    }
    
    public void derecha(){
        this.tankOne.girar(0);
        int i=0;
        boolean obstaculo = false;
        while(i<this.lstObs.size() && !obstaculo){
            if ((this.tankOne.getX()+5)+(this.tankOne.getAncho())>=this.lstObs.get(i).getX() 
                    && (this.tankOne.getX()+5)<=(this.lstObs.get(i).getX()+this.lstObs.get(i).getAncho())) {
                if (this.tankOne.getY()+(this.tankOne.getAlto())>=this.lstObs.get(i).getY() 
                        && this.tankOne.getY()<=(this.lstObs.get(i).getY()+this.lstObs.get(i).getAlto())) {
                    obstaculo = true;
                }
            }
            i++;
        }
        if (!obstaculo) {
            this.tankOne.setX(this.tankOne.getX()+5);
        }
    }
    
    public void izquierda(){
        this.tankOne.girar(1);;
        int i=0;
        boolean obstaculo = false;
        while(i<this.lstObs.size() && !obstaculo){
            if (this.tankOne.getX()-5>=this.lstObs.get(i).getX() 
                    && this.tankOne.getX()-5<=(this.lstObs.get(i).getX()+this.lstObs.get(i).getAncho())) {
                if (this.tankOne.getY()+(this.tankOne.getAlto())>=this.lstObs.get(i).getY() 
                        && this.tankOne.getY()<=(this.lstObs.get(i).getY()+this.lstObs.get(i).getAlto())) {
                    obstaculo = true;
                }
            }
            i++;
        }
        if (!obstaculo) {
            this.tankOne.setX(this.tankOne.getX()-5);
        }
    }
    
    public void arriba(){
        this.tankOne.girar(2);
        int i=0;
        boolean obstaculo = false;
        while(i<this.lstObs.size() && !obstaculo){
            if (this.tankOne.getX()+(this.tankOne.getAncho())>=this.lstObs.get(i).getX() 
                    && this.tankOne.getX()<=(this.lstObs.get(i).getX()+this.lstObs.get(i).getAncho())) {
                if (this.tankOne.getY()>=this.lstObs.get(i).getY() 
                        && (this.tankOne.getY()-5)<=(this.lstObs.get(i).getY()+this.lstObs.get(i).getAlto())) {
                    obstaculo = true;
                }
            }
            i++;
        }
        if (!obstaculo) {
            this.tankOne.setY(this.tankOne.getY()-5);
        }
    }
    
    public void abajo(){
        this.tankOne.girar(3);
        int i=0;
        boolean obstaculo = false;
        while(i<this.lstObs.size() && !obstaculo){
            if (this.tankOne.getX()+(this.tankOne.getAncho())>=this.lstObs.get(i).getX() 
                    && this.tankOne.getX()<=this.lstObs.get(i).getX()+this.lstObs.get(i).getAncho()) {
                if ((this.tankOne.getY()+5)+(this.tankOne.getAlto())>=this.lstObs.get(i).getY() 
                        && this.tankOne.getY()+5<=this.lstObs.get(i).getY()+this.lstObs.get(i).getAlto()) {
                    obstaculo = true;
                }
            }
            i++;
        }
        if (!obstaculo) {
            this.tankOne.setY(this.tankOne.getY()+5);
        }
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(this.stage.getImage(), 0, 0, this.getWidth(), this.getHeight(), this);
        if (this.tankOne != null) {
            g.drawImage(this.tankOne.getImagen().getImage(), this.tankOne.getX(),
                    this.tankOne.getY(), this.tankOne.getAncho(), this.tankOne.getAlto(), this);
        }
        if (this.lstObs != null) {
            for (int i = 4; i < this.lstObs.size(); i++) {
                g.drawImage(this.lstObs.get(i).getImagen().getImage(), this.lstObs.get(i).getX(),
                    this.lstObs.get(i).getY(), this.lstObs.get(i).getAncho(), this.lstObs.get(i).getAlto(), this);
            }
        }
        if (this.lstBalas != null) {
            for (int i = 0; i < this.lstBalas.size(); i++) {
                if (this.lstBalas.get(i).getEstado()) {
                    g.drawImage(this.lstBalas.get(i).getImagen().getImage(), this.lstBalas.get(i).getX(),
                        this.lstBalas.get(i).getY(), this.lstBalas.get(i).getAncho(), this.lstBalas.get(i).getAlto(), this);
                }  
            }
        }
        if(this.cli!=null){
            this.cli.enviar(this.tankOne.getX(), this.tankOne.getY(), this.tankOne.getDireccion());
        }
        
        repaint();
        
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}